import {
  DrawerForm,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import React from 'react';
import * as api from '@/services/geoip';
import { ProFormSelect } from '@ant-design/pro-components';

type FormProps = {
  open: boolean;
  current: API.GeoipIsp | undefined;
  parent: API.GeoipIsp | undefined;
  onCancel: () => void;
  onSubmit: (values: API.GeoipIsp) => Promise<void>;
};

const Form: React.FC<FormProps> = (props) => {
  const { open, current, parent, onCancel, onSubmit } = props;
  if (!open) {
    return null;
  }



  const loadDataAreas = async () => {
    const result = await api.list(null, null, null);

    let options = [];
    if (result && result.success) {
      for(let item of result.data){
        options.push({label:item.name, value: item.id});
      }
    }

    return options;
  };

  return (
    <DrawerForm<API.GeoipIsp>
      open={open}
      initialValues={current}
      title={`${current ? '编辑' : '添加'}ISP`}
      width={1000}
      onFinish={onSubmit}
      drawerProps={{
        // forceRender: true,
        // destroyOnClose: true,
        // closable: false,
        onClose: onCancel,
        maskClosable: false,
      }}
    >
      {parent ? 
        <ProFormSelect
          name="parentId"
          label="上级ISP"
          readonly={true}
          options={[{label: parent.name, value:parent.id}]}
          initialValue={parent.id}
        />
      :''}
      {!parent ?
        <ProFormSelect
          name="geoipId"
          label="地区"
          allowClear={true}
          request={loadDataAreas}
          rules={[{ required: true, message: '请选择地区' }]}
        />
      :''}
      <ProFormText
        name="name"
        label="名称"
        rules={[{ required: true, message: '请输入名称' }]}
        placeholder="请输入名称"
      />
      <ProFormTextArea 
        name="remark" 
        label="备注" 
        fieldProps={{ rows: 20 }}
      />
    </DrawerForm>
  );
};

export default Form;
